﻿using System.Security.Cryptography;
using System.Text;
using System.Web.Security;
using TestOnlineProject.Common;
using TestOnlineProject.Models.DataAccess;
using TestOnlineProject.Models.Entities;
using System;

namespace TestOnlineProject.Models.BusinessLogic
{
    public class AccountBLO
    {
        UserDAO userDAO = new UserDAO();
        StringHelper idStringSplitter = new StringHelper();

        /// <summary>
        /// Generate MD5 string.
        /// </summary>
        /// <param name="text">Input string</param>
        /// <returns>Output MD5 string</returns>

        public string GenerateMD5(string text)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));
            byte[] result = md5.Hash;
            var builder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                builder.Append(result[i].ToString("x2"));
            }
            return builder.ToString();
        }

        /// <summary>
        /// Log In.
        /// </summary>
        /// <param name="model">LogIn model</param>
        /// <returns>Login successful or not</returns>

        public int LogIn(AccountModel model)
        {
            try
            {
                // Generate MD5
                model.Password = GenerateMD5(model.Password);

                // Find User by Username and Password
                var user = userDAO.SelectByUsernamePassword(model.Username, model.Password);

                // Checking User
                if (user == null)
                {
                    return 0;
                }

                if (user.IsActivated == false)
                {
                    return 1;
                }

                // Valid
                FormsAuthentication.SetAuthCookie(user.UserId.ToString(), false);
                return 2;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// Log out.
        /// </summary>

        public void LogOut()
        {
            FormsAuthentication.SignOut();
        }

        /// <summary>
        /// Change password account
        /// </summary>
        /// <param name="user">User object</param>
        /// <param name="password">New password</param>
        /// <returns></returns>

        public int ChangePassword(User user, string password)
        {
            // Generate MD5 from new Password
            var newPassword = GenerateMD5(password);

            // Update new Password
            var us = userDAO.SelectByUserId(user.UserId);
            us.Password = newPassword;

            var result = userDAO.Update(us);
            return result;
        }

        /// <summary>
        /// Change Information of Account
        /// </summary>
        /// <param name="user">User object</param>
        /// <returns></returns>

        public int ChangeInfor(User user)
        {
            try
            {
                // Get User
                var us = userDAO.SelectByUserId(user.UserId);

                // Update Information
                us.FullName = user.FullName;
                us.ImageUrl = user.ImageUrl;
                if(us.RoleId == 1)
                {
                    us.Email = user.Email;
                }

                var result = userDAO.Update(us);
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// Reset Password for User
        /// </summary>
        /// <param name="user">User Object</param>
        /// <returns></returns>

        public bool ResetPassword(User user)
        {
            try
            {
                // Get new Password
                var newPassword = idStringSplitter.RandomString(8);

                // Update Password to db
                var us = userDAO.SelectByUserId(user.UserId);
                us.Password = GenerateMD5(newPassword);
                var result = userDAO.Update(user);
                if (result <= 0)
                {
                    return false;
                }

                // Send new Password to mail
                var send = idStringSplitter.SendMail(us.Email, newPassword);
                return send;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}